<?php
$csv_terminated = "\n";
    $csv_separator = ",";
    $csv_enclosed = '"';
    $csv_escaped = "\\";
	$fields_cnt =11;
	$schema_insert = '';
	$filename='coevents.csv';
$line = array('EventID','ComID','Event','EventDate','Position','FileName','TimeStamp','_declaration','_flagA','Note','EventDateFormat');
for ($i = 0; $i < $fields_cnt; $i++)
    {
        $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
            stripslashes($line[$i])) . $csv_enclosed;
        $schema_insert .= $l;
        $schema_insert .= $csv_separator;
    } // end for
 
    $out = trim(substr($schema_insert, 0, -1));
    $out .= $csv_terminated;
foreach ($coevents as $coevent){
	$schema_insert = '';
        for ($j = 0; $j < $fields_cnt; $j++)
        {
            //$codtrans['Codtran']['ComID']
			if ($coevent['Coevent'][$line[$j]] == '0' || $coevent['Coevent'][$line[$j]] != '')
            {
				if ($csv_enclosed == '')
					{
						$schema_insert .= $coevent['Coevent'][$line[$j]];
					} else
					{
						$schema_insert .= $csv_enclosed . 
						str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $coevent['Coevent'][$line[$j]]) . $csv_enclosed;
					}
			}
			else
            {
                $schema_insert .= '';
            }
 
            if ($j < $fields_cnt - 1)
            {
                $schema_insert .= $csv_separator;
            }
		}
		$out .= $schema_insert;
        $out .= $csv_terminated;
	}
    
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   header("Content-Length: " . strlen($out));
    // Output to browser with appropriate mime type, you choose ;)
    header("Content-type: text/x-csv");
    header("Content-Disposition: attachment; filename=$filename");
    echo $out;
    exit;
?>